<script setup>
import { ref, onMounted } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { BarChart, LineChart } from 'echarts/charts'
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  DataZoomComponent,
  ToolboxComponent
} from 'echarts/components'
import VChart from 'vue-echarts'

// 注册必要的组件
use([
  CanvasRenderer,
  BarChart,
  LineChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  DataZoomComponent,
  ToolboxComponent
])

// 拍摄记录列表
const captureRecords = ref([
  {
    id: 1,
    captureTime: '2024-01-20 14:30:00',
    status: 'success',
    result: '正常',
    imageUrl: '', // 后续可添加实际图片
    analysisData: {
      duration: '3秒',
      distribution: [
        { size: '0.5-1.0mm', percentage: 30 },
        { size: '1.0-1.5mm', percentage: 45 },
        { size: '1.5-2.0mm', percentage: 25 }
      ],
      uniformity: '95%',
      recommendation: '粒度分布均匀，符合标准要求'
    }
  },
  {
    id: 2,
    captureTime: '2024-01-20 15:45:00',
    status: 'warning',
    result: '待优化',
    imageUrl: '',
    analysisData: {
      duration: '3秒',
      distribution: [
        { size: '0.5-1.0mm', percentage: 20 },
        { size: '1.0-1.5mm', percentage: 55 },
        { size: '1.5-2.0mm', percentage: 25 }
      ],
      uniformity: '85%',
      recommendation: '中等粒度比例偏高，建议调整'
    }
  }
])

// 当前选中的记录
const selectedRecord = ref(null)

// 选择记录
const selectRecord = (record) => {
  selectedRecord.value = record
}

// 返回列表
const backToList = () => {
  selectedRecord.value = null
}

// 导出报告
const exportReport = () => {
  // 实现导出功能
}

// 删除记录
const deleteRecord = (record) => {
  captureRecords.value = captureRecords.value.filter(item => item.id !== record.id)
  if (selectedRecord.value?.id === record.id) {
    selectedRecord.value = null
  }
}

// 分析数据配置
const analysisChartOptions = {
  distribution: {
    title: {
      text: '粒度分布分析',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['直径比例', '面积比例'],
      top: 25
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['1', '2', '3', '4', '5', '6', '7'],
      name: '粒度等级'
    },
    yAxis: {
      type: 'value',
      name: '比例',
      axisLabel: {
        formatter: '{value}%'
      }
    },
    series: [
      {
        name: '直径比例',
        type: 'bar',
        data: [39.61, 20.64, 24.25, 11.82, 3.50, 0.18, 0.001],
        itemStyle: {
          color: '#3498db'
        }
      },
      {
        name: '面积比例',
        type: 'bar',
        data: [12.68, 17.06, 30.20, 25.85, 13.35, 0.85, 0.006],
        itemStyle: {
          color: '#2ecc71'
        }
      }
    ]
  },
  
  errors: {
    title: {
      text: '误差分析',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['误差0', '误差1'],
      top: 25
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['1', '2', '3', '4', '5', '6', '7'],
      name: '粒度等级'
    },
    yAxis: {
      type: 'value',
      name: '误差值'
    },
    series: [
      {
        name: '误差0',
        type: 'line',
        data: [-1.68, -13.06, -6.20, -1.85, 6.65, 13.15, 2.99],
        smooth: true,
        itemStyle: {
          color: '#e74c3c'
        }
      },
      {
        name: '误差1',
        type: 'line',
        data: [-28.61, -16.64, -0.25, 12.18, 16.50, 13.82, 3.00],
        smooth: true,
        itemStyle: {
          color: '#9b59b6'
        }
      }
    ]
  }
}
</script>

<template>
  <div class="result-container">
    <!-- 记录列表视图 -->
    <template v-if="!selectedRecord">
      <a-card class="list-card">
        <template #title>
          <a-space>
            <icon-history />
            拍摄记录
          </a-space>
        </template>
        
        <a-table :data="captureRecords" :pagination="false" class="record-table">
          <template #columns>
            <a-table-column title="拍摄时间" data-index="captureTime">
              <template #cell="{ record }">
                <a-space>
                  <icon-calendar />
                  {{ record.captureTime }}
                </a-space>
              </template>
            </a-table-column>
            
            <a-table-column title="分析结果" data-index="result">
              <template #cell="{ record }">
                <a-tag :color="record.status === 'success' ? 'green' : 'orange'">
                  {{ record.result }}
                </a-tag>
              </template>
            </a-table-column>
            
            <a-table-column title="操作" align="right">
              <template #cell="{ record }">
                <a-space>
                  <a-button type="text" @click="selectRecord(record)">
                    查看详情
                  </a-button>
                  <a-button type="text" status="danger" @click="deleteRecord(record)">
                    删除
                  </a-button>
                </a-space>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </a-card>
    </template>

    <!-- 详细分析结果视图 -->
    <template v-else>
      <div class="detail-header">
        <a-space>
          <a-button type="text" @click="backToList">
            <template #icon><icon-left /></template>
            返回列表
          </a-button>
          <a-divider direction="vertical" />
          <span class="capture-time">
            拍摄时间：{{ selectedRecord.captureTime }}
          </span>
        </a-space>
      </div>

      <a-row :gutter="16">
        <!-- 图像展示 -->
        <a-col :span="12">
          <a-card class="image-card">
            <template #title>拍摄图像</template>
            <div class="image-placeholder">
              <icon-image :style="{ fontSize: '64px' }" />
              <p>图像预览</p>
            </div>
          </a-card>
        </a-col>

        <!-- 分析数据 -->
        <a-col :span="12">
          <a-card class="analysis-card">
            <template #title>分析结果</template>
            
            <!-- 基础数据 -->
            <a-descriptions :data="[
              { label: '分析用时', value: selectedRecord.analysisData.duration },
              { label: 'JS散度', value: '0.27 / 0.41' },
              { label: '均方误差', value: '0.006 / 0.025' },
              { label: '平均绝对误差', value: '0.065 / 0.130' }
            ]" />

            <!-- 粒度分布图表 -->
            <a-divider>粒度分布</a-divider>
            <v-chart 
              class="chart" 
              :option="analysisChartOptions.distribution" 
              autoresize 
            />

            <!-- 误差分析图表 -->
            <a-divider>误差分析</a-divider>
            <v-chart 
              class="chart" 
              :option="analysisChartOptions.errors" 
              autoresize 
            />
          </a-card>
        </a-col>
      </a-row>

      <!-- 操作按钮 -->
      <div class="detail-actions">
        <a-space>
          <a-button type="primary" @click="exportReport">
            <template #icon><icon-download /></template>
            导出报告
          </a-button>
          <a-button>
            <template #icon><icon-printer /></template>
            打印结果
          </a-button>
        </a-space>
      </div>
    </template>
  </div>
</template>

<style scoped>
.result-container {
  padding: 16px;
  height: 100%;
}

.list-card {
  height: 100%;
}

.record-table {
  margin-top: 16px;
}

.detail-header {
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.capture-time {
  color: var(--color-text-2);
  font-size: 14px;
}

.image-card, .analysis-card {
  height: calc(100vh - 280px);
}

.image-placeholder {
  height: calc(100% - 48px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--color-fill-2);
  border-radius: 4px;
  color: var(--color-text-3);
}

.image-placeholder p {
  margin-top: 16px;
}

.distribution-chart {
  margin: 16px 0;
}

.distribution-item {
  margin-bottom: 16px;
}

.distribution-item span {
  display: inline-block;
  width: 80px;
  margin-right: 16px;
  color: var(--color-text-2);
}

.recommendation {
  display: none;
}

.detail-actions {
  margin-top: 16px;
  text-align: center;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .result-container {
    padding: 8px;
  }
  
  .image-card, .analysis-card {
    height: auto;
    margin-bottom: 16px;
  }
}

/* 添加图表相关样式 */
.chart {
  height: 300px;
  margin: 16px 0;
}

.analysis-card {
  height: auto;
  min-height: calc(100vh - 280px);
}
</style>
